package ext.acae2.baseline.web;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;

import wt.log4j.LogR;
import wt.util.WTException;
import wt.util.WTProperties;
import wt.vc.baseline.ManagedBaseline;

public class DownloadCompareBaseline {

    private static final Logger LOGGER = LogR.getLogger(DownloadCompareBaseline.class.getName());
    
    public static void download(ManagedBaseline baselineA, ManagedBaseline baselineB, OutputStream outputStream) 
            throws WTException, IOException {
        LOGGER.debug("> Start download");
        
        String wthome = (String) WTProperties.getLocalProperties().get("wt.home");
        
        File templateFile = new File(wthome + File.separator 
                + "codebase" + File.separator + "ext" + File.separator 
                + "acae2" + File.separator + "conf" + File.separator
                + "CompareBaseline.xls");
        
        HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(templateFile));
        HSSFSheet sheet = workbook.getSheetAt(0);
        
        String baselineANumber = baselineA.getNumber();
        String baselineBNumber = baselineB.getNumber();
        HSSFRow rowfirst = sheet.getRow(1);
        rowfirst.getCell(2).setCellValue(baselineANumber);
        rowfirst.getCell(5).setCellValue(baselineBNumber);
        
        List<Map<String, String>> compareResult = CompareBaselineReport.getCompareResultMap(baselineA, baselineB);
        int rowNum = 3;
        for (Map<String, String> rowData : compareResult) {
            HSSFRow row = sheet.createRow(rowNum);
            rowNum++;
            
            row.createCell(0).setCellValue(rowData.get(CompareBaselineReport.KEY_INDEX));
            
            String keyANumber = rowData.get(CompareBaselineReport.KEY_A_NUMBER);
            if (keyANumber.indexOf("img") >= 0) {
                keyANumber = keyANumber.substring(keyANumber.indexOf(">") + 1);
            }
            
            row.createCell(1).setCellValue(keyANumber);
            row.createCell(2).setCellValue(rowData.get(CompareBaselineReport.KEY_A_NAME));
            row.createCell(3).setCellValue(rowData.get(CompareBaselineReport.KEY_A_VERSION));
            
            String keyBNumber = rowData.get(CompareBaselineReport.KEY_B_NUMBER);
            if (keyBNumber.indexOf("img") >= 0) {
                keyBNumber = keyBNumber.substring(keyBNumber.indexOf(">") + 1);
            }
            
            row.createCell(4).setCellValue(keyBNumber);
            row.createCell(5).setCellValue(rowData.get(CompareBaselineReport.KEY_B_NAME));
            row.createCell(6).setCellValue(rowData.get(CompareBaselineReport.KEY_B_VERSION));
            row.createCell(7).setCellValue(rowData.get(CompareBaselineReport.KEY_COMPARE_RESULT));
        }
        
        workbook.write(outputStream);
        outputStream.flush();
        outputStream.close();
        
        LOGGER.debug("> End download");
    }
}
